/**
 * Copyright (C), 2020-2020,贵州铭明网络科技有限公司
 * FileName: PositionInfoServiceImpl
 * Author:   杨朝湖
 * Date:     2020/12/8 15:42
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.mm.service.impl;

import com.mm.bo.InsertPosiInfoBo;
import com.mm.bo.PosiListInfoBo;
import com.mm.bo.PosiUpdateBo;
import com.mm.mapper.LogsInfoMapper;
import com.mm.mapper.PositionInfoMapper;
import com.mm.org.n3r.idworker.Sid;
import com.mm.pojo.AdministratorInfo;
import com.mm.pojo.LogsInfo;
import com.mm.pojo.PositionInfo;
import com.mm.redis.RedisService;
import com.mm.rediskey.AdminKey;
import com.mm.service.PositionInfoService;
import com.mm.util.PageUtil;
import com.mm.vo.PosiListInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Gym
 * @create 2020/12/8
 * @since 1.0.0
 */
@Service
public class PositionInfoServiceImpl implements PositionInfoService {

    @Resource
    private PositionInfoMapper positionInfoMapper;
    @Resource
    private Sid sid;
    @Resource
    private RedisService redisService;
    @Resource
    private LogsInfoMapper logsInfoMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertPosiInfo(InsertPosiInfoBo insertPosiInfoBo) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, insertPosiInfoBo.getToken(), AdministratorInfo.class);
        PositionInfo positionInfo = new PositionInfo();
        BeanUtils.copyProperties(positionInfo, insertPosiInfoBo);
        positionInfo.setPosiId(sid.nextShort());
        //执行添加方法
        int rowPosi = positionInfoMapper.insertSelective(positionInfo);
        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "添加了职位：" + insertPosiInfoBo.getPosiName();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return rowPosi;
    }

    @Override
    public PageUtil<PosiListInfoVo> queryPosiList(PosiListInfoBo posiListInfoBo) {
        //查询总数
        Integer count = positionInfoMapper.queryPosiListCount(posiListInfoBo);
        //查询列表
        List<PosiListInfoVo> posiListInfoVos = positionInfoMapper.queryPosiList(posiListInfoBo);
        PageUtil<PosiListInfoVo> page = new PageUtil<>(count, count, posiListInfoBo.getPageIndex(), posiListInfoBo.getPageSize(), posiListInfoVos);
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updatePosiInfo(PosiUpdateBo posiUpdateBo) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, posiUpdateBo.getToken(), AdministratorInfo.class);
        PositionInfo positionInfo = new PositionInfo();
        BeanUtils.copyProperties(positionInfo, posiUpdateBo);
        int rowPosi = positionInfoMapper.updateByPrimaryKeySelective(positionInfo);
        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //查询当前职位信息
        PositionInfo positionInfoDB = positionInfoMapper.selectByPrimaryKey(posiUpdateBo.getPosiId());
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "修改了职位：" + positionInfoDB.getPosiName() + "的基本信息";
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return rowPosi;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deletePosiInfo(String posiId, String token) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, token, AdministratorInfo.class);
        //查询当前职位信息
        PositionInfo positionInfoDB = positionInfoMapper.selectByPrimaryKey(posiId);
        positionInfoDB.setPosiStatus(0);
        int rowPosi = positionInfoMapper.updateByPrimaryKeySelective(positionInfoDB);
        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "删除了职位：" + positionInfoDB.getPosiName();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return rowPosi;
    }
}